package fr.gadcparis.envieparisienne.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.util.Log;
import fr.gadcparis.envieparisienne.fusiontables.ftclient.ClientLogin;
import fr.gadcparis.envieparisienne.fusiontables.ftclient.FtClient;

public class FtDataClient {

	private static final String TAG = "FtDataClient";
	protected static final int RAYON_METRE = 1000;

	protected FtClient ftclient;
	private Geocoder geocoder;
	private List<Hit> results;
	private int nbHits;

	public FtDataClient(Context context) {

	    // Initialize FTClient
	    String token = ClientLogin.authorize("enviesparisiennes@gmail.com", "HackathonPaug");
	    ftclient = new FtClient(token);
	    geocoder =  new Geocoder(context, Locale.getDefault());
		this.results = new ArrayList<Hit>();
	}
	
	protected String getAdresse(Location location) {
		String adresse="";
	     try {
	        // Get the first address in the list of possible addresses for
	        // the lat/lon coordinates
	        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),
	            location.getLongitude(), 1);

	        // If an address was returned, concatenate the results into
	        // a string
	        StringBuilder formattedAddress = new StringBuilder();
	        if (!addresses.isEmpty()) {
	          Address address = addresses.get(0);

	          for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
	            formattedAddress.append(address.getAddressLine(i) + " ");
	          }
	          adresse = formattedAddress.toString();
	        }
	      } catch (IOException e) {
	          //
	    	  e.printStackTrace();
	      }
		return adresse;
	}
	
	protected String[] getHeader(String string) {
    	String[] header = string.split(",");
		return header;
	}
	
	protected void parseResults(String string) {
		String[] toto = string.split("\n");  
		String[] header = null;
		String str = null;
		for (String line: toto) {
			Log.i("TOTO", "line: " + line);
			if (str == null) {
				str = line;
				header = this.getHeader(str);
			}
			else {
				Hit hit = (Hit) HitFt.fromString(header,line);
				results.add(hit);
				this.nbHits++;
			}
		}
		/*BufferedReader reader = new BufferedReader(
		  new StringReader(string));
		boolean FirstSkip = false;       
		String[] header = null;
		try {
			this.nbHits=0;
			  while ((str = reader.readLine()) != null) {
			        if (!FirstSkip) {
			        	// Parse Header
			        	header = this.getHeader(str);
			        	FirstSkip = true;
			        	continue;     
			        }
					Hit hit = (Hit) HitFt.fromString(header,str);
					results.add(hit);
					this.nbHits++;
			          //if (str.length() > 0) System.out.println(str.charAt(0));
			  }
		} catch(IOException e) {
		  e.printStackTrace();
		}					*/
		Log.i(TAG, "parseResults succeed");
	}	
	
	public Hit getHit(int i) {
		if (i >= 0 && i < this.count()) {
			return results.get(i);
		}
		// TODO throw exception
		return null;
	}
	
	public int count() {
		if (this.nbHits < 0) {
			this.nbHits = this.results.size();
		}
		return this.nbHits;
	}
}
